package com.flyxteam.commons.dao.mybatis.core.sqlbuilder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.flyxteam.commons.dao.mybatis.api.exception.MybatisDataAccessException;

/**
 * INSERT SQL构建器
 * @author yuzh
 *
 */
public class InsertSqlBuilder extends AbstractSqlBuilder {
	
	/** 插入字段--值map */
	private Map<String, Object> columnMap = new HashMap<String, Object>();
	/** 插入字段--值集合 */
	private List<Map<String, Object>> columnList = new ArrayList<Map<String, Object>>();
	
	public InsertSqlBuilder(String tableName, Map<String, Object> columnMap) {
		this.tableName = tableName;
		this.columnMap = columnMap;
	}
	
	public InsertSqlBuilder(String tableName, List<Map<String, Object>> columnList) {
		this.tableName = tableName;
		this.columnList = columnList;
	}
	
	@Override
	public String buildSqlString() {
		String sql = null;
		if(columnMap != null && !columnMap.isEmpty()) {	// 单条插入
			sql = buildInsertColumn(columnMap, 0);
		} else if(columnList != null && !columnList.isEmpty()) {		// 批量插入
			sql = buildInsertBatchColumn();
		} else {
			LOGGER.error("insert object is null");
			throw new MybatisDataAccessException("insert object is null");
		}
		
		LOGGER.info("sql is {}, param is {}", sql.toString(), getSqlParam());
		return sql.toString();
	}
	
	/**
	 * 拼装单条插入字段信息
	 * @param m
	 * @param index
	 */
	private String buildInsertColumn(Map<String, Object> m, int index) {
		// insert into tableName (column1, column2, column3) values(value1, value2, value3)
		
		// 字段名
		StringBuilder columnSql = new StringBuilder("(");
		// 字段值
		StringBuilder valueSql = new StringBuilder("(");
		Set<Entry<String,Object>> columnSet = m.entrySet();
		int columnSize = columnSet.size();
		int columnPos = 0;
		for (Entry<String, Object> column : columnSet) {
			columnSql.append(column.getKey());
			valueSql.append("#{").append(column.getKey()).append("_insert_").append(index).append("}");
			sqlParam.put(column.getKey() + "_insert_" + index, column.getValue());
			
			columnPos++;
			if(columnPos < columnSize) {
				columnSql.append(", ");
				valueSql.append(", ");
			}
		}
		columnSql.append(")");
		valueSql.append(")");
		
		StringBuilder sql = new StringBuilder();
		sql.append("insert into ")
				.append(tableName)
				.append(" ")
				.append(columnSql)
				.append(" values ")
				.append(valueSql);
		
		return sql.toString();
	}
	
	/**
	 * 拼装批量插入字段信息
	 * @param columnSql
	 * @param valueSql
	 */
	private String buildInsertBatchColumn() {
		/*
		 *  insert into tableName (column1, column2, column3) values (value1, value2, value3); 
		 *  insert into tableName (column1, column2, column3) values (value1, value2, value3);
		 */
		StringBuilder sql = new StringBuilder();
		for(int i = 0, size = columnList.size(); i < size; i++) {
			sql.append(buildInsertColumn(columnList.get(i), i)).append("; ");
		}

		return sql.toString();
	}
}
